package com.sun.login;

import android.app.Activity;
import android.content.Context;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sun.lib.callback.LoginCallBack;
import com.sun.login.bean.LoginEvent;
import com.sun.login.fragment.LoginBackFragment;

import org.greenrobot.eventbus.EventBus;

/*
 * Created by Administrator on 2019/7/30.
 *
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG              #
 * #                                                   #
 */
public class LoginHelper {
    private final String TAG = "LoginHelper";
    private LoginBackFragment fragment;

    public LoginHelper(FragmentActivity activity) {
        fragment = getLoginBackFragment(activity);
    }

    private LoginBackFragment getLoginBackFragment(FragmentActivity activity) {
        LoginBackFragment backFragment = findLoginBackFragment(activity);
        boolean isNewInstance = backFragment == null;
        if (isNewInstance) {
            backFragment = new LoginBackFragment();
            FragmentManager manager = activity.getSupportFragmentManager();
            manager.beginTransaction().add(backFragment, TAG)
                    .commitAllowingStateLoss();
            manager.executePendingTransactions();
        }
        return backFragment;
    }

    private LoginBackFragment findLoginBackFragment(FragmentActivity activity) {
        return (LoginBackFragment) activity.getSupportFragmentManager().findFragmentByTag(TAG);
    }

    public void login(LoginCallBack callBack) {
        if (fragment != null)
            fragment.login(callBack);
    }

    public static void loginSuccess() {
        EventBus.getDefault().post(new LoginEvent(true));
    }
}
